<template>
  <div>
    <div class="head">
      <van-search
        v-model="ky"
        placeholder="请输入搜索关键词"
        shape="round"
        @input="input"
      />
    </div>
    <div class="main" v-if="ky.length == 0">
      <div>热门搜索</div>
      <div class="word">
        <div class="word-item" v-for="(item, index) in hotkeyword" :key="index">
          {{ item.first }}
        </div>
      </div>
    </div>

    <div class="list" v-if="ky.length > 0">
      <div class="t">搜索“{{ ky }}”</div>
      <ul>
        <li v-for="(v, i) in keywordList" :key="i">
          <van-icon name="search" /> <span>{{ v.keyword }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { hot, suggest } from "@/api/home/home";
export default {
  data() {
    return {
      ky: "",
      hotkeyword: [],
      keywordList: [],
      timer: null,
    };
  },
  mounted() {
    hot().then((res) => {
      if (res.status == 200) {
        this.hotkeyword = res.data.result.hots;
      }
    });
  },
  methods: {
    input() {
      clearTimeout(this.timer);
      if (this.ky.length > 0) {
        this.timer = setTimeout(() => {
          suggest({ keywords: this.ky, type: "mobile" }).then((res) => {
            if (res.status == 200) {
              this.keywordList = res.data.result.allMatch;
            }
          });
        }, 50);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}
.main {
  padding: 10px;
  font-size: 14px;
  .word {
    display: flex;
    flex-wrap: wrap;
    .word-item {
      padding: 5px 10px;
      border: 1px solid rgb(230, 219, 219);
      border-radius: 10px;
      margin: 5px;
    }
  }
}
.list {
  font-size: 15px;
  padding: 0 10px;
  .t {
    color: skyblue;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  ul {
    li {
      height: 45px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);
      line-height: 45px;
    }
  }
}
</style>